<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Leave extends Model
{
    //
	protected $table='leave';
	protected $fillable=[
		'staff_id','name','uid','staff_task_id','type','start_time','end_time','time','comment','status','store_id','store_station_id','date_time','scheduling_id'
	];

	public function listStore($store_id){
		return $this->where(['store_id'=>$store_id,'status'=>1])->with(['staff_info'=>function($query){
			$query->with('store_station');
		}])->get();
	}

	public function desc($id){
		return $this->where(['id'=>$id])->with(['staff_info'=>function($query){
			$query->with('store_station');
		}])->first();
	}

	public function all_leavel($store_id,$time){
		$data=$this->where(['store_id'=>$store_id,'date_time'=>$time])->get();
		$newArr=[];
		foreach($data as $kk=>$vv){
			$newArr[$vv['staff_id']][$kk]['staff_id']=$vv['staff_id'];
			$newArr[$vv['staff_id']][$kk]['start_time']=date('H:i:s',strtotime($vv['start_time']));
			$newArr[$vv['staff_id']][$kk]['end_time']=date('H:i:s',strtotime($vv['end_time']));
		}
		return $newArr;
	}

	//	中止详情
	public function stop_desc($staff_task_id,$firstday,$lastday,$staff_id){
		$data=$this->where(['staff_id'=>$staff_id])->where(function ($query)use($firstday,$lastday){
			$query->whereBetween('start_time',[$firstday,$lastday]);

		})->get();
		foreach($data as $kk=>$vv){
			$data[$kk]['created']=date("Y-m-d",strtotime($vv['created_at']));
		}
		return $data;
	}

	public function staff_info(){
		return $this->belongsTo('App\Model\Staff','staff_id');
	}

	public function all_leave_data($where){
		return $this->where(['store_id'=>$where['store_id'],'store_station_id'=>$where['store_station_id']])->with('staff_info')
			->whereBetween('start_time',[$where['start_time'],$where['end_time']])
			->get();
	}

	public function allLeave($where){
		$data= $this->where(['store_id'=>$where['store_id']])
			->where(function ($query) use($where){
				if($where['store_station_id'] !=null){
					$query->where(['store_station_id'=>$where['store_station_id']]);
				}
			})
			->whereBetween('start_time',[$where['start_time'],$where['end_time']])->get();
		if(!count($data)){
			return null;
		}
		$newArr=[];
//		return $newArr;
		$newArr['a']['count']=0;$newArr['a']['message']='家有喜事';$newArr['a']['color']='#E06D06';
		$newArr['b']['count']=0;$newArr['b']['message']='身体不适';$newArr['b']['color']='#FF9901';
		$newArr['c']['count']=0;$newArr['c']['message']='调休';$newArr['c']['color']='#FFC53A';
		$newArr['d']['count']=0;$newArr['d']['message']='其它';$newArr['d']['color']='#FFE281';
		$newArr['all']['count']=0;$newArr['all']['message']='全部';
		foreach($data as $k=>$v){
			if($v['type']==1){
				$newArr['a']['count']++;
			}
			if($v['type']==2){
				$newArr['b']['count']++;
			}
			if($v['type']==3){
				$newArr['c']['count']++;
			}
			if($v['type']==4){
				$newArr['d']['count']++;
			}
			$newArr['all']['count']++;
		}

		$staff=new Staff();
		$store_peple=$staff->allStaff($where['store_id']);
//		计算百分比

		$newArr['a']['countb']=$this->jiisuan($newArr['a']['count'],$newArr['all']['count']);
		$newArr['b']['countb']=$this->jiisuan($newArr['b']['count'],$newArr['all']['count']);
		$newArr['c']['countb']=$this->jiisuan($newArr['c']['count'],$newArr['all']['count']);
		$newArr['d']['countb']=$this->jiisuan($newArr['d']['count'],$newArr['all']['count']);

		$str['data']=[$newArr['a'],$newArr['b'],$newArr['c'],$newArr['d']];
		$str['ing']=[];
		$str['ing']['leavel_num']=$newArr['all']['count'];
		$str['ing']['num']=$store_peple;


		return $str;
	}


//	计算百分比
	public function jiisuan($a,$b){
		return number_format($a/$b,2)*100 .'%';
	}

	public function store_station(){
		return $this->belongsTo('App\Model\Store_station','station');
	}


	public function store(){
		return $this->belongsTo('App\Model\Store','store_id');
	}

	public function store_station_(){
		return $this->belongsTo('App\Model\Store_station','store_station_id');
	}

	public function leave_type(){
		return $this->belongsTo('App\Model\Leave_type','type');
	}

//	获取任务中止天数
	public function leave_day($staff_task_id){
		return $this->where(['staff_task_id'=>$staff_task_id])->pluck('date_time');
	}


}
